package com.yzh.springboot_rbac.controller;

import com.yzh.springboot_rbac.entity.DictType;
import com.yzh.springboot_rbac.service.DictTypeService;
import com.yzh.springboot_rbac.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型管理菜单
 */
@RestController
@RequestMapping("/dictType")
public class DictTypeController {
    
    @Autowired
    private DictTypeService dictTypeService;
    /**
     * 1.添加字典类型
     */
    @PostMapping("/add")
    public AjaxResult addDictType(@RequestBody DictType dictType) {
        int row = dictTypeService.insertDictType(dictType);
        if (row > 0) {
            return AjaxResult.success(dictType);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 2.查询所有类型字典
     */
    @PostMapping("/selectAll")
    public AjaxResult selectAllDictType() {
        List<DictType> dictTypeList = dictTypeService.selectDictTypeAll();
        if (dictTypeList.isEmpty()) {
            return AjaxResult.error();
        } else {
            return AjaxResult.success(dictTypeList);
        }
    }

    /**
     * 3.根据条件查询字典
     */
    @PostMapping("/selectByCondition")
    public AjaxResult selectDictTypeByCondition(@RequestBody DictType dictType) {
        List<DictType> dictTypeList = dictTypeService.selectDictTypeByConditon(dictType);
        if (dictTypeList.isEmpty()) {
            return AjaxResult.error("字典不存在");
        } else {
            return AjaxResult.success(dictTypeList);
        }
    }

    /**
     * 4.修改字典类型
     */
    @PostMapping("/update")
    public AjaxResult updateDictType(@RequestBody DictType dictType) {
        int row = dictTypeService.updateDictType(dictType);
        if (row > 0) {
            return AjaxResult.success(dictType);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 5.删除字典类型
     */
    @DeleteMapping("/delete/{dictIds}")
    public AjaxResult deletDictType(@PathVariable("dictIds") long[] dictIds) {
        Boolean flag = dictTypeService.deleteDictTypeByIds(dictIds);
        if (flag) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 6.清除字典缓存
     */
    @PostMapping("/clear")
    public AjaxResult clearDictCache() {
        dictTypeService.clearDictCache();
        return AjaxResult.success();
    }

    /**
     * 7.重置字典缓存
     */
    @PostMapping("/reset")
    public AjaxResult resetDictCache() {
        dictTypeService.resetDictCache();
        return AjaxResult.success();
    }
}
